<%= lines(autogen_notice(:go, pwd)) -%>

package google

import (
<%- # We list all the v2 imports here and unstable imports, because we run 'goimports' to guess the correct
    # set of imports, which will never guess the major version correctly. -%>
  "github.com/apparentlymart/go-cidr/cidr"
  "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
  "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
  "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
  "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
  "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
  "github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
  "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
  "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
  "github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
  "google.golang.org/api/bigtableadmin/v2"
)

<%
    resource_name = product_ns + object.name
    properties = object.all_user_properties
    # See discussion on asset name here: https://github.com/GoogleCloudPlatform/magic-modules/pull/1520
    asset_name_template = '//' + product_ns.downcase + '.googleapis.com/' + (!object.self_link.nil? && !object.self_link.empty? ? object.self_link : object.base_url + '/{{name}}')
    version_regex = /\/(v\d[^\/]*)\//
    api_version = version_regex.match?(asset_name_template) ? version_regex.match(asset_name_template)[1] : @base_url.split("/")[-1]
    asset_name_template.gsub!(version_regex, '/')
%>

<%= lines(compile(pwd + '/' + object.custom_code.constants)) if object.custom_code.constants -%>

func Get<%= resource_name -%>CaiObject(d TerraformResourceData, config *Config) ([]Asset, error) {
    name, err := assetName(d, config, "<%= asset_name_template -%>")
    if err != nil {
        return []Asset{}, err
    }
    if obj, err := Get<%= resource_name -%>ApiObject(d, config); err == nil {
        return []Asset{{
            Name: name,
            Type: "<%= product_ns.downcase -%>.googleapis.com/<%= object.name -%>",
            Resource: &AssetResource{
                Version: "<%= api_version -%>",
                DiscoveryDocumentURI: "https://www.googleapis.com/discovery/v1/apis/<%= product_ns.downcase -%>/<%= api_version -%>/rest",
                DiscoveryName: "<%= object.name -%>",
                Data: obj,
            },
        }}, nil
    } else {
        return []Asset{}, err
    }
}

func Get<%= resource_name -%>ApiObject(d TerraformResourceData, config *Config) (map[string]interface{}, error) {
    obj := make(map[string]interface{})
<%  object.settable_properties.each do |prop| -%>
<%    if prop.flatten_object -%>
    <%= prop.api_name -%>Prop, err := expand<%= resource_name -%><%= titlelize_property(prop) -%>(nil, d, config)
<%    else # if flatten -%>
    <%= prop.api_name -%>Prop, err := expand<%= resource_name -%><%= titlelize_property(prop) -%>(d.Get("<%= prop.name.underscore -%>"), d, config)
<%    end -%>
    if err != nil {
        return nil, err
<%      unless prop.send_empty_value -%>
    } else if v, ok := d.GetOkExists("<%= prop.name.underscore -%>"); !isEmptyValue(reflect.ValueOf(<%= prop.api_name -%>Prop)) && (ok || !reflect.DeepEqual(v, <%= prop.api_name -%>Prop)) {
<%      else -%>
    } else if v, ok := d.GetOkExists("<%= prop.name.underscore -%>"); ok || !reflect.DeepEqual(v, <%= prop.api_name -%>Prop) {
<%      end -%>
        obj["<%= prop.api_name -%>"] = <%= prop.api_name -%>Prop
    }
<%  end -%>

<%  if object.custom_code.encoder -%>
    return resource<%= resource_name -%>Encoder(d, config, obj)
<%  else -%>
    return obj, nil
<%  end -%>
}

<% if object.custom_code.encoder -%>
func resource<%= resource_name -%>Encoder(d TerraformResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
<%= lines(compile(pwd + '/' + object.custom_code.encoder)) -%>
}
<% end -%>

<% object.settable_properties.each do |prop| -%>
<%= lines(build_expand_method(resource_name, prop, object, pwd), 1) -%>
<% end -%>

